<template>
  <div>
    <div style="margin-top:20px" />
    <el-dialog
      v-if="dialogFormVisible"
      v-el-drag-dialog
      :class="{'el-dialog_wrapper_modal_none':!isModal}"
      class="el-dialog_wrapper_submit_handle"
      :append-to-body="!isModal"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :width="dialogWidth"
      :modal="isModal"
      :lock-scroll="isModal"
      :title="title"
      @close="closeDialog"
    >
      <div class="submit_handle-el_dialog">
        <el-scrollbar v-loading="loading" :native="false" tag="div">
          <SubmitHandle v-if="wfJsonData!=undefined&&submitHandle!=null" :shwo-over-path="showOverPath" :cur-t-n="curTN" :wf-instance="wfInstance" :submit-handle="submitHandle" :wf-json-data="wfJsonData" :common-idea-list="commonIdeaList" :submit-template-list="submitTemplateList" :no-idea-submit-template="noIdeaSubmitTemplate" />
        </el-scrollbar>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button plain icon="el-icon-close" type="default" @click="closeDialog">取 消</el-button>
        <el-button icon="el-icon-check" type="primary" @click="submitFile">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import SubmitHandle from './SubmitHandle'

export default {
    name: 'SubmitDialog',
    components: {
        SubmitHandle
    },
    props: {
        curTN: { type: Object, default: function() { return {} } },
        wfInstance: { type: Object, default: function() { return {} } },
        wfJsonData: { type: Object, default: function() { return {} } },
        submitHandle: { type: Object, default: function() { return {} } },
        commonIdeaList: { type: Array, default: function() { return {} } },
        submitTemplateList: { type: Array, default: function() { return {} } },
        noIdeaSubmitTemplate: { type: Boolean, default: false },
        isModal: { type: Boolean, default: false },
        showOverPath: { type: Boolean, default: true },
        dialogWidth: { type: String, default: '1000px' }
    },
    data() {
        return {
            title: '提交办理',
            dialogFormVisible: false,
            loading: true,
            submitMode: ''
        }
    },
    computed: {

    },
    watch: {

    },
    created() {
        this.$bus.on('btnStartDoingClick', this.btnStartDoingClick)
        this.$bus.on('submitHanddleUpdate', this.submitHanddleUpdate)
    },
    beforeDestroy() {
        this.$bus.off('btnStartDoingClick', this.btnStartDoingClick)
        this.$bus.off('submitHanddleUpdate', this.submitHanddleUpdate)
    },
    mounted() {

    },
    methods: {
        btnStartDoingClick() {
            const formEdit = this.$vnode.context.canEdit
            const $scope = this.$vnode.context
            const _this = this
            this.submitMode = $scope.submitMode

            this.formValidate(() => {
                if ($scope.wfInstance.status < 1 && ($scope.bodyfile === null || $scope.bodyfile.name === '') && $scope.bodyFileEditor > 1 && $scope.fileIsMust !== 0) {
                    let errmsg = ''
                    if ($scope.bodyFileEditor === 2 || $scope.bodyFileEditor === 4) {
                        errmsg = '请编辑正文！'
                    } else {
                        errmsg = '请上传正文！'
                    }
                    $scope.activeName = 'second'
                    $scope.$alert(errmsg, '操作提示', { type: 'warning' })
                    return
                }

                if (!_this.wfJsonData || !_this.wfJsonData.routes) {
                    const wfInstance = JSON.parse(JSON.stringify(_this.wfInstance))
                    wfInstance.passenger.busiData = $scope.vo
                    _this.loading = true
                    _this.dialogFormVisible = true

                    _this.$nextTick(() => {
                        this.$wfEngine.getWfJsonData($scope, wfInstance, $scope.wfParams).then(rst => {
                            _this.loading = false
                        })
                    })
                } else {
                    _this.dialogFormVisible = true
                    _this.loading = false
                }
            }, (tip, event) => {
                $scope.activeName = 'first'
                this.$nextTick(() => {
                    if (event) {
                        const eventPosition = event.getBoundingClientRect()
                        const scrolltop = document.querySelector('.mainform-container .el-scrollbar__wrap').scrollTop
                        document.querySelector('.mainform-container .el-scrollbar__wrap').scrollTop = scrolltop + (eventPosition.top - 102)
                    }
                })
            }, formEdit)
        },
        submitHanddleUpdate() {
            const dialog = document.querySelector('.el-dialog_wrapper_submit_handle')
            const dialog_div = dialog.querySelector('.submit_handle-el_dialog')
            if (dialog.offsetHeight > 600) {
                dialog_div.style.height = 600 - 167 + 'px'
            }
        },
        submitFile() {
            const $scope = this.$vnode.context
            let submitBtn = $scope.commandBar.filter(btn => {
                if (btn.btnJavascriptBak) {
                    return true
                }
            })[0]

            submitBtn = JSON.parse(JSON.stringify(submitBtn))

            let btnClick = submitBtn.btnJavascriptBak
            if (btnClick.indexOf('(') !== -1) {
                let btnParams = btnClick.substring(btnClick.indexOf('(') + 1, btnClick.lastIndexOf(')'))
                if (btnParams !== '') {
                    btnParams = btnParams.replace(/'/g, '').split(',')
                    submitBtn.btnParams = btnParams
                }
                btnClick = btnClick.split('(')[0]
            }
            if ($scope[btnClick] !== undefined) {
                submitBtn.btnClick = $scope[btnClick]
            }

            if (submitBtn.btnParams !== undefined && submitBtn.btnParams !== null) {
                submitBtn.btnClick.apply(this.$parent, submitBtn.btnParams)
            } else {
                submitBtn.btnClick.apply(this.$parent)
            }
        },
        closeDialog() {
            this.dialogFormVisible = false
        }
    }
}
</script>

<style scoped lang="scss">
.submit-handle-container{
    margin-top:20px;
}
 /deep/ .el-dialog__body{
    .submit-handle-container{
        margin-top:0;
    }
}

.submit_handle-el_dialog{
    overflow-y:auto;
    position: relative;

    &::before{
        content: '';
        position: absolute;
        top: 0;
        //border-top:1px solid #EBEEF5;
        width: 100%;
        z-index:1;
    }
    &::after{
        content: '';
        position: absolute;
        bottom: 0;
        //border-bottom:1px solid #EBEEF5;
        width: 100%;
        z-index:1;
    }
    .el-scrollbar {
        width: 100%;
        height: 100%;
        min-height: 200px;

        &::before{
            content: '';
            position: absolute;
            right: 0;
            //border-right:1px solid #EBEEF5;
            height: 100%;
            z-index:1;
        }
        /deep/ .el-scrollbar__wrap {
            overflow-x: hidden;
            margin-bottom:0!important;
        }
    }
}
</style>
